package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.HttpStatusEnum;
import com.example.common.codeFactory.CodeGenerateStrategy;
import com.example.common.codeFactory.CodeGenerateStrategyFactory;
import com.example.common.codeFactory.entity.CodeGenerateType;
import com.example.entity.*;
import com.example.pojo.Rule;
import com.example.mapper.RuleMapper;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 权限角色表(Rule)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class RuleService {
    // mapper对象
    @Resource
    private RuleMapper ruleMapper;
    // 编号工厂
    @Resource
    private CodeGenerateStrategyFactory codeGenerateStrategyFactory;

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param rule        筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, Rule rule) {
        QueryWrapper<Rule> qw = new QueryWrapper<>();
        //TODO: getByPage rule qw查询条件待构建
        IPage ipage = new Page(currentPage, pageSize);
        ruleMapper.selectPage(ipage, qw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()) {
            ipage = new Page(ipage.getPages(), pageSize);
            ruleMapper.selectPage(ipage, qw);
        }
        if (ipage.getTotal() <= 0) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), ipage);
    }

    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        Rule rule = ruleMapper.selectById(id);
        if (rule == null || rule.getId() == null) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), rule);
    }

    /**
     * 新增数据
     *
     * @param rule 实体
     * @return 新增结果
     */
    public ResponseData add(Rule rule) {
        //插入
        if (ruleMapper.insert(rule) > 0) {
            // 插入成功后要赋值编码
            CodeGenerateStrategy codeGenerateStrategy = codeGenerateStrategyFactory.getCodeGenerateStrategy(CodeGenerateType.RULE);
            String codePrefix = rule.getParent().equals("-1") ? "" : rule.getParent();
            rule.setCode(codeGenerateStrategy.generateCode(codePrefix, rule.getId().intValue()));
            ruleMapper.updateById(rule);
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "新增失败，请稍后再试");
    }

    /**
     * 更新数据
     *
     * @param rule 实体
     * @return 更新结果
     */
    public ResponseData update(Rule rule) {
        // 更新的时候需要关注code的更新，这里默认都重新赋值
        CodeGenerateStrategy codeGenerateStrategy = codeGenerateStrategyFactory.getCodeGenerateStrategy(CodeGenerateType.RULE);
        String codePrefix = rule.getParent().equals("-1") ? "" : rule.getParent();
        rule.setCode(codeGenerateStrategy.generateCode(codePrefix, rule.getId().intValue()));
        //更新
        if (ruleMapper.updateById(rule) > 0) {
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return
     */
    public ResponseData delete(Long id) {
        if (ruleMapper.deleteById(id) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList) {
        if (ruleMapper.deleteBatchIds(idList) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 树形选项列表查询
     * @param parent
     * @param deleted
     * @return
     */
    public ResponseData getIndentOptions(String parent, Integer deleted) {
        // 查询
        SelectOptionsList optionsList = new SelectOptionsList();
        // 整理成带缩进的列表
        List<TreeOptions> treeOptions = ruleMapper.selectTreeOptions(parent, deleted);
        optionsList.makeIndentOptions(treeOptions);
        List<Map> ruleOptions = optionsList.getIndentOptionsList();
        if (ruleOptions == null || ruleOptions.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), ruleOptions);
    }

    /**
     * 树形表格查询
     * @param rule
     * @return
     */
    public ResponseData getTreeTable(Rule rule) {
        // 重新构建查询参数
        QueryRule queryRule = new QueryRule();
        queryRule.init(rule);
        // 查询
        List<TreeRuleTable> treeRuleTables = ruleMapper.selectTreeRuleTable(queryRule);
        if (treeRuleTables == null || treeRuleTables.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), treeRuleTables);
    }
}
